﻿Imports System.Data
Imports VSModel
Imports Cls_Common
Imports SecurityService

Partial Class Control_MaSoThue_Update
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "Sub and Function"
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            hidID.Value = Request.QueryString("Certifiedid")
            If hidID.Value <> "" Then
                BindToGrid()
            End If


        End If
    End Sub

    Private Sub BindToGrid(Optional ByVal iPage As Integer = 1, Optional ByVal strSearch As String = "")
        Using data As New VSEntities

            'So ban ghi muon the hien tren trang
            Dim intPag_Size As Int32 = drpPage_Size.SelectedValue
            Dim p = (From q In data.CertifiedHouseholds Where q.CertifiedID = hidID.Value Order By q.CertifiedID Descending Select q).Skip((iPage - 1) * intPag_Size).Take(intPag_Size).ToList
            'show CMND va MST neu co
            Dim _j = (From q In data.CertifiedHouseholds Where q.CertifiedID = hidID.Value Order By q.CertifiedID Descending Select q).FirstOrDefault
            If Not _j Is Nothing Then
                txtGiayChungNhan.Text = IIf(IsNothing(_j.Ow_CardID) = True, "", _j.Ow_CardID)
                txtMaSoThue.Text = IIf(IsNothing(_j.Ow_TaxCode) = True, "", _j.Ow_TaxCode)
            End If
            Dim strKey_Name() As String = {"CertifiedID", "CertifiedCode", "CertifiedStatus", "StatusId", "Ow_Sex", "Biz_VietName"}
            'Tong so ban ghi
            hidCount.Value = (From q In data.CertifiedHouseholds Where q.CertifiedID = hidID.Value Order By q.CertifiedID Descending Select q).Count
            Create_Pager(hidCount.Value, iPage, intPag_Size, 10)
            With grdShow
                .DataKeyNames = strKey_Name
                .DataSource = p
                .DataBind()
            End With
            lblTotal.Text = "Hiển thị " + (((iPage - 1) * intPag_Size) + 1).ToString + " đến " + (((iPage - 1) * intPag_Size) + grdShow.Rows.Count).ToString + " trong tổng số " + hidCount.Value.ToString + " bản ghi."
        End Using
    End Sub
    Sub Create_Pager(ByVal Total_Record As Integer, ByVal Page_Index As Integer, ByVal Page_Size As Integer, ByVal Page2Show As Integer)
        Dim TotalPage As Integer = IIf((Total_Record Mod Page_Size) = 0, Total_Record / Page_Size, Total_Record \ Page_Size + 1)
        'lu lai tong so ban ghi
        hidIndex_page.Value = TotalPage
        'gan lai curPage de set active
        hidCur_Page.Value = Page_Index
        'generate ra left page
        Dim cPageGenerate_left As IEnumerable(Of Integer)
        If Page_Index <= Page2Show Then
            cPageGenerate_left = Enumerable.Range(1, Page_Index)
        Else
            cPageGenerate_left = Enumerable.Range(Page_Index - Page2Show, Page2Show)
        End If
        'generate ra right page
        Dim cPageGenerate_Right As IEnumerable(Of Integer)
        If Page_Index + Page2Show <= TotalPage Then
            cPageGenerate_Right = Enumerable.Range(Page_Index, Page2Show + 1)
        Else
            cPageGenerate_Right = Enumerable.Range(Page_Index, TotalPage - Page_Index + 1)
        End If
        'union 2 range va bind to Grid
        With rptPage
            .DataSource = cPageGenerate_left.Union(cPageGenerate_Right)
            .DataBind()
        End With
    End Sub
    Protected Sub rptPage_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptPage.ItemDataBound
        Dim lnkTitle As LinkButton
        lnkTitle = e.Item.FindControl("lnkTitle")
        'Dim ScriptManager As System.Web.UI.ScriptManager = System.Web.UI.ScriptManager.GetCurrent(Me.Page)
        'ScriptManager.RegisterAsyncPostBackControl(lnkTitle)
        If e.Item.DataItem = hidCur_Page.Value Then
            lnkTitle.Text = "<span class='current'>" & e.Item.DataItem & "</span>"
        Else
            lnkTitle.Text = "<span>" & e.Item.DataItem & "</span>"
        End If
        lnkTitle.ToolTip = e.Item.DataItem
    End Sub
    Protected Sub lnkTitle_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim lnkTile As LinkButton = CType(sender, LinkButton)
        BindToGrid(CInt(lnkTile.ToolTip))
        lnkLast.Enabled = True
        lnkFirst.Enabled = True
        If CInt(lnkTile.ToolTip) = hidIndex_page.Value Then
            lnkLast.Enabled = False
        ElseIf CInt(lnkTile.ToolTip) = 1 Then
            lnkFirst.Enabled = False
        End If
    End Sub
    Protected Sub lnkFirst_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFirst.Click
        If hidCur_Page.Value > 1 Then
            hidCur_Page.Value = hidCur_Page.Value - 1
        End If
        BindToGrid(hidCur_Page.Value)
    End Sub
    Protected Sub lnkLast_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkLast.Click
        hidCur_Page.Value = hidCur_Page.Value + 1
        BindToGrid(hidCur_Page.Value)
    End Sub
#End Region
#Region "Event for control"
    Protected Sub grdShow_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdShow.RowDataBound
        If e.Row.RowIndex >= 0 Then
            Dim lblSTT As Label = CType(e.Row.FindControl("lblSTT"), Label)
            lblSTT.Text = CInt(drpPage_Size.SelectedValue) * (CInt(hidCur_Page.Value) - 1).ToString + e.Row.RowIndex + 1

            Dim hplBiz_VietName As HyperLink = CType(e.Row.FindControl("hplBiz_VietName"), HyperLink)
            hplBiz_VietName.NavigateUrl = "../../Page/MaSoThue/Detail.aspx?Certifiedid=" & grdShow.DataKeys(e.Row.RowIndex)("CertifiedID").ToString
            hplBiz_VietName.Text = grdShow.DataKeys(e.Row.RowIndex)("Biz_VietName")

            Using data As New VSEntities
                Dim lblStatusId As Label = CType(e.Row.FindControl("lblStatusId"), Label)
                Dim _l As Integer = grdShow.DataKeys(e.Row.RowIndex)("StatusId")
                lblStatusId.Text = (From h In data.Status Where h.StatusId = _l Select h.TitleHoSo).FirstOrDefault
                lblStatusId.CssClass = "hoso_" & _l.ToString

                
            End Using
            'copy cho nhanh, dung co gang go~, vi tu nhien no nho thoi, copy cho do sai
            Dim lblGioTinh As Label = CType(e.Row.FindControl("lblGioTinh"), Label)
            lblGioTinh.Text = IIf(grdShow.DataKeys(e.Row.RowIndex)("Ow_Sex") = 1, "Nam", "Nữ")
        End If
    End Sub
#End Region
#Region "Search"
    Protected Sub drpPage_Size_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles drpPage_Size.SelectedIndexChanged
        BindToGrid(1, )
    End Sub
#End Region
    Protected Sub btnCapNhat_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCapNhat.Click
        Using data As New VSEntities
            Dim p = (From q In data.CertifiedHouseholds Where q.CertifiedID = hidID.Value Order By q.CertifiedID Descending Select q).FirstOrDefault
            If Not p Is Nothing Then
                Try
                    p.Ow_TaxCode = txtMaSoThue.Text.Trim()
                    If Not txtMaSoThue.Text.Trim() Is Nothing AndAlso txtMaSoThue.Text.Trim() <> "" Then
                        p.IsMaSoThue = 1
                    Else
                        p.IsMaSoThue = 0
                    End If
                    p.NgayCapNhatMST = Now
                    data.SaveChanges()

                    Excute_Javascript("Alertbox('Cập nhật thành công.');window.location ='../../Page/MaSoThue/Detail.aspx?Certifiedid=" & Request("Certifiedid") & "';", Me.Page, True)

                Catch ex As Exception
                    log4net.Config.XmlConfigurator.Configure()
                    Excute_Javascript("Alertbox('Cập nhật thất bại.');window.location ='../../Page/MaSoThue/List.aspx';", Me.Page, True)
                End Try
            End If
        End Using
    End Sub

    Protected Sub btnCkeck_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCkeck.Click
        Using data As New VSEntities
            Dim _check As String = txtGiayChungNhan.Text.Trim()
            Dim _checkMST As String = txtMaSoThue.Text.Trim()
            Dim p = (From q In data.CertifiedHouseholds
                     Where (_check.Contains(q.Ow_CardID) Or _checkMST.Contains(q.Ow_TaxCode)) Order By q.CertifiedDate Descending Select q).FirstOrDefault
            If Not p Is Nothing And p.CertifiedID <> 0 Then
                Dim Url As String = MainDomain & "Page/MaSoThue/Update.aspx?Certifiedid=" & p.CertifiedID
                Response.Redirect(Url)
            End If
        End Using
    End Sub
End Class
